using BuildingBlocks.Core.Entities;
using System.Text.RegularExpressions;

namespace User.Domain.ValueObjects;
public class PhoneNumber : ValueObject
{
    public string Value { get; } = default!;

    private PhoneNumber() { } // for EF

    private PhoneNumber(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("Phone number cannot be empty.", nameof(value));

        if (!IsValidPhoneNumber(value))
            throw new ArgumentException("Invalid phone number format.", nameof(value));

        Value = value;
    }

    public static PhoneNumber From(string value) => new PhoneNumber(value);

    public override string ToString() => Value;

    private static bool IsValidPhoneNumber(string number)
    {
        // 允许 +86, 12345678901 这种格式，简单示意，实际根据业务需求改进
        return Regex.IsMatch(number, @"^\+?[0-9]{7,15}$");
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Value;
    }
}